<?PHP
global $vbulletin, $userdata, $user;
if ($vbulletin->options['neots_enable_product']) {
	if($userdata) {
	$neotsuid = $userdata->fetch_field('neots_uid');
	}
	if($neotsuid != NULL) {
		$neotsarray = unserialize($neotsuid);
		$user['userid'] = $userdata->fetch_field('userid');
		$user['usergroupid'] = $userdata->fetch_field('usergroupid');
		$user['membergroupids'] = $userdata->fetch_field('membergroupids');
		$usergroups_changed = $userdata->usergroups_changed();
		$vbops = $vbulletin->options;
		$membergroupids = fetch_membergroupids_array($user, TRUE);
		$ts_sgroup = array();
		foreach($membergroupids as $groupid) {
			if($groupid) {
				$ts_squery = $vbulletin->db->query_first("SELECT neots_sgroup FROM " . TABLE_PREFIX . "usergroup WHERE usergroupid = " . $groupid);
				if ($ts_squery['neots_sgroup'] != NULL) {
					$ts_squery = unserialize($ts_squery['neots_sgroup']);
					foreach($ts_squery as $ts_squery => $sgid) {
						$ts_sgroup[$sgid] = $sgid;
					}
				}
			}
		}
		foreach($membergroupids as $groupid) {
			if($groupid) {
				$ts_cquery = $vbulletin->db->query_first("SELECT neots_cgroup FROM " . TABLE_PREFIX . "usergroup WHERE usergroupid = " . $groupid);
				if ($ts_cquery['neots_cgroup']) {
					$ts_cquery = unserialize($ts_cquery['neots_cgroup']);
					foreach($ts_cquery as $cid => $cgid) {
						if($cgid != '0') {
							$ts_cgroup[$cid] = $cgid;
						}
					}
				}
			}
		}
		if($usergroups_changed) {
			$olduser['userid'] = $userdata->existing['userid'];
			$olduser['usergroupid'] = $userdata->existing['usergroupid'];
			$olduser['membergroupids'] = $userdata->existing['membergroupids'];
			$oldmembergroupids = fetch_membergroupids_array($olduser, TRUE);
			$old_ts_sgroup = array();
			foreach($oldmembergroupids as $groupid) {
				$old_ts_squery = $vbulletin->db->query_first("SELECT neots_sgroup FROM " . TABLE_PREFIX . "usergroup WHERE usergroupid = " . $groupid);
				if ($old_ts_squery['neots_sgroup']) {
					$old_ts_squery = unserialize($old_ts_squery['neots_sgroup']);
					foreach($old_ts_squery as $old_ts_group) {
						$old_ts_sgroup = array_merge((array)$old_ts_sgroup, (array)$old_ts_group);
					}
				}
			}
			$diff_old_ts_sgroup = array_diff($old_ts_sgroup, $ts_sgroup);
		}
		/* load framework library */
		require_once("./teamspeak/TeamSpeak3.php");
		/* initialize */
		TeamSpeak3::init();
		try {
			$ts3_VirtualServer = TeamSpeak3::factory("serverquery://".$vbops['neots_uname'].":".$vbops['neots_pass']."@".$vbops['neots_ip'].":".$vbops['neots_port']."/?server_port=".$vbops['neots_id']);
			foreach($neotsarray as $ts_cluid) {
				if($ts_cluid != NULL) {
					$ts_cldbid = $ts3_VirtualServer->clientFindDb($ts_cluid, uid); 
					if($ts_cldbid != NULL) {
						$ts_cldbid = $ts_cldbid[0];
						if(is_array($ts_sgroup)) {
							if($usergroups_changed) {
								foreach($diff_old_ts_sgroup as $old_ts_sgroup) {
									if($old_ts_sgroup != '2') {
										$oldlist = $ts3_VirtualServer->serverGroupClientList($old_ts_sgroup);
										if ($oldlist[$ts_cldbid]) {
											$ts3_VirtualServer->serverGroupClientDel($old_ts_sgroup, $ts_cldbid);
										}
									}
								}
							}
							foreach($ts_sgroup as $ts_sgroupid) {
								$currentlist = $ts3_VirtualServer->serverGroupClientList($ts_sgroupid);
								if (!$currentlist[$ts_cldbid]) {
									$ts3_VirtualServer->serverGroupClientAdd($ts_sgroupid, $ts_cldbid);
								}
							}
						}
						if(is_array($ts_cgroup)) {
							foreach($ts_cgroup as $cid => $cgid) { 
								if($cgid != '0') {
									$ts3_VirtualServer->clientSetChannelGroup($ts_cldbid, $cid, $cgid); 
								}
							} 
						}
						$neotsproperties = array('client_description' => 'VB Username: '.$userdata->fetch_field('username').' | VB UserID: '.$userdata->fetch_field('userid'));
						$ts3_VirtualServer->clientModifyDb($ts_cldbid, $neotsproperties);
					}
				}
			}
		}
		catch(Exception $e)
		{
			if($e->getCode() != '1281') {
			eval(standard_error(fetch_error('neo_teamspeak_error', $e->getCode(), $e->getMessage())));
			}
		}
	}
}
?>